package com.example.homework;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.homework.model.Contact;
import com.example.homework.model.User;

import java.util.ArrayList;
import java.util.List;

public class DBHelper extends SQLiteOpenHelper {
    public static String DB_NAME = "contact";
    public static int DB_VERSION = 1;

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table contacts (id integer primary key autoincrement," +
                "firstname text," +
                "lastname text," +
                "phone text," +
                "blocked integer);";
        String userSql="create table users (id integer primary key autoincrement," +
                "username text," +
                "password text," +
                "sex text);";
        db.execSQL(sql);
        db.execSQL(userSql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void del(SQLiteDatabase db,int id){
        db.execSQL("delete from contacts where id="+id);
        db.close();
    }
    public void updateBlock(SQLiteDatabase db,int id,boolean block){
        int blockValue=block?1:0;
        db.execSQL("update contacts set blocked="+blockValue+" where id="+id);
        db.close();
    }

    public void insert(SQLiteDatabase db, Contact contact){
        ContentValues contentValues=new ContentValues();
        contentValues.put("firstname",contact.getFirstName());
        contentValues.put("lastname",contact.getLastName());
        contentValues.put("phone",contact.getPhone());
        contentValues.put("blocked",contact.isBlocked());
        db.insert("contacts",null,contentValues);
        db.close();
    }

    public void insertUser(SQLiteDatabase db, User user){
        ContentValues contentValues=new ContentValues();
        contentValues.put("username",user.getName());
        contentValues.put("password",user.getPassword());
        contentValues.put("sex",user.getSex());
        db.insert("users",null,contentValues);
        db.close();
    }
    @SuppressLint("Range")
    public User findByName(SQLiteDatabase db, String username){
        String sql="select * from users where username="+username;
        Cursor cursor=db.rawQuery(sql,null);
        boolean hasNext=cursor.moveToNext();
        if (!hasNext){
            return null;
        }
        User user=new User();
        user.setId(cursor.getInt(cursor.getColumnIndex("id")));
        user.setName(cursor.getString(cursor.getColumnIndex("username")));
        user.setPassword(cursor.getString(cursor.getColumnIndex("password")));
        cursor.close();
        return user;
    }

    public List<Contact> queryAll(SQLiteDatabase db){
        String sql="select * from contacts";
        Cursor cursor=db.rawQuery(sql,null);
        List<Contact> contacts=new ArrayList<>();
        while (cursor.moveToNext()){
            @SuppressLint("Range") int id=cursor.getInt(cursor.getColumnIndex("id"));
            @SuppressLint("Range") String firstname=cursor.getString(cursor.getColumnIndex("firstname"));
            @SuppressLint("Range") String lastname=cursor.getString(cursor.getColumnIndex("lastname"));
            @SuppressLint("Range") String phone=cursor.getString(cursor.getColumnIndex("phone"));
            @SuppressLint("Range") boolean blocked= cursor.getInt(cursor.getColumnIndex("blocked")) > 0;
            Contact contact=new Contact(id,firstname,lastname,phone,blocked);
            contacts.add(contact);
        }
        cursor.close();
        return contacts;
    }
}
